﻿/*
 * 版 本 v1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2017.04.18
 * 描 述：部门选择
 */
var dfopid = request('dfopid');
var selectValue = request('selectValue');
var selectedRow;
var refreshGirdData;
var acceptClick;

var companyId = request("companyId");
var keyword = request("keyword");
var controlName = request("controlName");
var btnIndex = request("btnIndex");

var bootstrap = function ($, learun) {
    "use strict";
    var bmitem = {value: '', text: ''};

    var page = {
        init: function () {
            page.bind();
            page.initGrid();
            page.search({keyword: keyword});
        },
        bind: function () {
            // 查询
            $('#btn_Search').on('click', function () {
                var keyword = $('#txt_Keyword').val();
                page.search({keyword: keyword});
            });
            $("#txt_Keyword").keypress(function (even) {
                if (even.which == 13) {
                    var keyword = $(this).val();
                    page.search({keyword: keyword});
                }
            });
            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });
        },
        initGrid: function () {
            $('#gridtable').jfGrid({
                url: config.base_server() + 'departments?access_token=' + config.getToken().access_token,
                headData: [
                    {label: '编号', name: 'code', width: 160, align: 'left'},
                    {label: '名称', name: 'name', width: 300, align: 'left'},
                    {label: '名称', name: 'codeAndName', width: 300, align: 'left', hidden: true},
                ],
                dblclick: function (row) {
                    bmitem = {value: row.code, text: row.codeAndName};
                    if (learun.frameTab.currentIframe().bm != undefined) {
                        learun.frameTab.currentIframe().bm = bmitem;
                    }
                    if(!!btnIndex){
                        parent.$(".layui-layer-btn0")[btnIndex].click();
                    } else {
                        parent.$(".layui-layer-btn0")[1].click();
                    }

                    //关闭layer弹出层
                    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                    parent.layer.close(index);
                },
                isPage: true,
                reloadSelected: true,
                isTree: false,
                mainId: 'id'
            });
        },
        search: function (param) {
            param = param || {};
            param.companyId = companyId;
            param.status = 1;
            // param.keyword = keyword;
            $('#gridtable').jfGridSet('reload', param);
        }
    };
    refreshGirdData = function () {
        page.search();
    };

    // 保存数据
    acceptClick = function (callBack) {
        var value = $('#gridtable').jfGridValue('code');
        var text = $('#gridtable').jfGridValue('codeAndName');
        var item = {value: value, text: text};
        if (learun.checkrow(value)) {
            callBack(item, dfopid);
            return true;
            // dialogClose();
        } else {
            // learun.alert.warning('请选择部门！');
            return false;
        }
    };
    page.init();
}
